package cn.z2huo.mybatis.generator.mybatis3dynamicsql.dao;

import cn.z2huo.demo.model.dataobject.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.*;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import static cn.z2huo.mybatis.generator.mybatis3dynamicsql.dao.UserDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

@Mapper
public interface UserMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<User>, CommonUpdateMapper {
    BasicColumn[] selectList = BasicColumn.columnList(id, createTime, operateTime, createByCode, operateByCode, userCode, userName, validDate, invalidDate, validFlag, deleteFlag, companyCode, departmentCode);

    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @Results(id="UserResult", value = {
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="operate_time", property="operateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="create_by_code", property="createByCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="operate_by_code", property="operateByCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_code", property="userCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
        @Result(column="valid_date", property="validDate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="invalid_date", property="invalidDate", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="valid_flag", property="validFlag", jdbcType=JdbcType.VARCHAR),
        @Result(column="delete_flag", property="deleteFlag", jdbcType=JdbcType.VARCHAR),
        @Result(column="company_code", property="companyCode", jdbcType=JdbcType.VARCHAR),
        @Result(column="department_code", property="departmentCode", jdbcType=JdbcType.VARCHAR)
    })
    List<User> selectMany(SelectStatementProvider selectStatement);

    @SelectProvider(type=SqlProviderAdapter.class, method="select")
    @ResultMap("UserResult")
    Optional<User> selectOne(SelectStatementProvider selectStatement);

    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, user, completer);
    }

    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, user, completer);
    }

    default int deleteByPrimaryKey(Long id_) {
        return delete(c -> 
            c.where(id, isEqualTo(id_))
        );
    }

    default int insert(User row) {
        return MyBatis3Utils.insert(this::insert, row, user, c ->
            c.map(id).toProperty("id")
            .map(createTime).toProperty("createTime")
            .map(operateTime).toProperty("operateTime")
            .map(createByCode).toProperty("createByCode")
            .map(operateByCode).toProperty("operateByCode")
            .map(userCode).toProperty("userCode")
            .map(userName).toProperty("userName")
            .map(validDate).toProperty("validDate")
            .map(invalidDate).toProperty("invalidDate")
            .map(validFlag).toProperty("validFlag")
            .map(deleteFlag).toProperty("deleteFlag")
            .map(companyCode).toProperty("companyCode")
            .map(departmentCode).toProperty("departmentCode")
        );
    }

    default int insertMultiple(Collection<User> records) {
        return MyBatis3Utils.insertMultiple(this::insertMultiple, records, user, c ->
            c.map(id).toProperty("id")
            .map(createTime).toProperty("createTime")
            .map(operateTime).toProperty("operateTime")
            .map(createByCode).toProperty("createByCode")
            .map(operateByCode).toProperty("operateByCode")
            .map(userCode).toProperty("userCode")
            .map(userName).toProperty("userName")
            .map(validDate).toProperty("validDate")
            .map(invalidDate).toProperty("invalidDate")
            .map(validFlag).toProperty("validFlag")
            .map(deleteFlag).toProperty("deleteFlag")
            .map(companyCode).toProperty("companyCode")
            .map(departmentCode).toProperty("departmentCode")
        );
    }

    default int insertSelective(User row) {
        return MyBatis3Utils.insert(this::insert, row, user, c ->
            c.map(id).toPropertyWhenPresent("id", row::getId)
            .map(createTime).toPropertyWhenPresent("createTime", row::getCreateTime)
            .map(operateTime).toPropertyWhenPresent("operateTime", row::getOperateTime)
            .map(createByCode).toPropertyWhenPresent("createByCode", row::getCreateByCode)
            .map(operateByCode).toPropertyWhenPresent("operateByCode", row::getOperateByCode)
            .map(userCode).toPropertyWhenPresent("userCode", row::getUserCode)
            .map(userName).toPropertyWhenPresent("userName", row::getUserName)
            .map(validDate).toPropertyWhenPresent("validDate", row::getValidDate)
            .map(invalidDate).toPropertyWhenPresent("invalidDate", row::getInvalidDate)
            .map(validFlag).toPropertyWhenPresent("validFlag", row::getValidFlag)
            .map(deleteFlag).toPropertyWhenPresent("deleteFlag", row::getDeleteFlag)
            .map(companyCode).toPropertyWhenPresent("companyCode", row::getCompanyCode)
            .map(departmentCode).toPropertyWhenPresent("departmentCode", row::getDepartmentCode)
        );
    }

    default Optional<User> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, user, completer);
    }

    default List<User> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, user, completer);
    }

    default List<User> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, user, completer);
    }

    default Optional<User> selectByPrimaryKey(Long id_) {
        return selectOne(c ->
            c.where(id, isEqualTo(id_))
        );
    }

    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, user, completer);
    }

    static UpdateDSL<UpdateModel> updateAllColumns(User row, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalTo(row::getId)
                .set(createTime).equalTo(row::getCreateTime)
                .set(operateTime).equalTo(row::getOperateTime)
                .set(createByCode).equalTo(row::getCreateByCode)
                .set(operateByCode).equalTo(row::getOperateByCode)
                .set(userCode).equalTo(row::getUserCode)
                .set(userName).equalTo(row::getUserName)
                .set(validDate).equalTo(row::getValidDate)
                .set(invalidDate).equalTo(row::getInvalidDate)
                .set(validFlag).equalTo(row::getValidFlag)
                .set(deleteFlag).equalTo(row::getDeleteFlag)
                .set(companyCode).equalTo(row::getCompanyCode)
                .set(departmentCode).equalTo(row::getDepartmentCode);
    }

    static UpdateDSL<UpdateModel> updateSelectiveColumns(User row, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(id).equalToWhenPresent(row::getId)
                .set(createTime).equalToWhenPresent(row::getCreateTime)
                .set(operateTime).equalToWhenPresent(row::getOperateTime)
                .set(createByCode).equalToWhenPresent(row::getCreateByCode)
                .set(operateByCode).equalToWhenPresent(row::getOperateByCode)
                .set(userCode).equalToWhenPresent(row::getUserCode)
                .set(userName).equalToWhenPresent(row::getUserName)
                .set(validDate).equalToWhenPresent(row::getValidDate)
                .set(invalidDate).equalToWhenPresent(row::getInvalidDate)
                .set(validFlag).equalToWhenPresent(row::getValidFlag)
                .set(deleteFlag).equalToWhenPresent(row::getDeleteFlag)
                .set(companyCode).equalToWhenPresent(row::getCompanyCode)
                .set(departmentCode).equalToWhenPresent(row::getDepartmentCode);
    }

    default int updateByPrimaryKey(User row) {
        return update(c ->
            c.set(createTime).equalTo(row::getCreateTime)
            .set(operateTime).equalTo(row::getOperateTime)
            .set(createByCode).equalTo(row::getCreateByCode)
            .set(operateByCode).equalTo(row::getOperateByCode)
            .set(userCode).equalTo(row::getUserCode)
            .set(userName).equalTo(row::getUserName)
            .set(validDate).equalTo(row::getValidDate)
            .set(invalidDate).equalTo(row::getInvalidDate)
            .set(validFlag).equalTo(row::getValidFlag)
            .set(deleteFlag).equalTo(row::getDeleteFlag)
            .set(companyCode).equalTo(row::getCompanyCode)
            .set(departmentCode).equalTo(row::getDepartmentCode)
            .where(id, isEqualTo(row::getId))
        );
    }

    default int updateByPrimaryKeySelective(User row) {
        return update(c ->
            c.set(createTime).equalToWhenPresent(row::getCreateTime)
            .set(operateTime).equalToWhenPresent(row::getOperateTime)
            .set(createByCode).equalToWhenPresent(row::getCreateByCode)
            .set(operateByCode).equalToWhenPresent(row::getOperateByCode)
            .set(userCode).equalToWhenPresent(row::getUserCode)
            .set(userName).equalToWhenPresent(row::getUserName)
            .set(validDate).equalToWhenPresent(row::getValidDate)
            .set(invalidDate).equalToWhenPresent(row::getInvalidDate)
            .set(validFlag).equalToWhenPresent(row::getValidFlag)
            .set(deleteFlag).equalToWhenPresent(row::getDeleteFlag)
            .set(companyCode).equalToWhenPresent(row::getCompanyCode)
            .set(departmentCode).equalToWhenPresent(row::getDepartmentCode)
            .where(id, isEqualTo(row::getId))
        );
    }
}